<!--
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2022-06-20 12:14:49
 * @LastEditors: voanit
 * @LastEditTime: 2022-06-20 12:30:17
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /*
    for
    forEach
    filter
    map
    findIndex
    find
    some
    reduce
    every
    */
    var arr = [
      { id: 1, name: 'jack', age: 18 },
      { id: 2, name: 'tom', age: 20 },
      { id: 3, name: 'jerry', age: 22 }
    ]
    /*  filter */
    var newArr = arr.filter(item => {
      if (item.age > 18) {
        return true
      }
    })
    // console.log(newArr, 111);
    /* map */
    var newArr2 = arr.map(item => {
      return {
        id: item.id,
        name: item.name,
        age: item.age + 1
      }
    })
    // console.log(newArr2, 222);
    /*  findIndex    返回满足条件的索引,只返回一个 */
    var index = arr.findIndex(item => {
      if (item.age > 18) {
        return true
      }
    })
    // console.log(index, 333);
    /* find  返回满足条件的这一项  是一个对象 */
    var obj = arr.find(item => {
      if (item.id > 2) {
        return true
      }
    })
    // console.log(obj, 444);
    /* some  返回一个bool只要有一个满足就终止 */
    var flag = arr.some(item => {
      if (item.age > 16) {
        return true
      }
    })
    // console.log(flag, 555);
    /* every 每一项必须满足，只要有一项不满足就是false */
    var flag2 = arr.every(item => {
      if (item.age > 17) {
        return true
      }
    })
    // console.log(flag2, 666);
    /* reduce((sum,item)=>{},0)*/
    var total = arr.reduce((sum, item) => sum + item.age, 0)
    console.log(total, 777);


  </script>
</body>

</html>